import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from sklearn.metrics import silhouette_score
from sklearn.cluster import KMeans
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.metrics import silhouette_score
from matplotlib import font_manager
from kmeans import KMeansClassifier



#用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei']
#用来正常显示负号
plt.rcParams['axes.unicode_minus'] = False
import matplotlib.pyplot as plt
def loadDataset():
    """
    加载数据集(DataFrame格式),并转换成所需要的格式。
    最后将返回为一个numpy的数组类型
    """
    filename = "beijing.xlsx"
    # 自定义数据的行列索引（行索引使用pd默认的，列索引使用自定义的）
    names = [
        "id","title","area","toward","total_price",
        "unit_price","city","reach","lng","lat"
    ]
    # 自定义需要处理的缺失值标记列表
    miss_value = ["null", "暂无数据"]
    # 数据类型会自动转换
    # 使用自定义的列名，跳过文件中的头行，处理缺失值列表标记的缺失值
    df = pd.read_excel(filename, skiprows=[0], names=names, na_values=miss_value)#跳过表头
    data_cluster = df[["id", "total_price", "unit_price", "area","lat","lng"]]#创建表头名
    # 剔除带有空值的行
    data_cluster = data_cluster.dropna()
    # # # 去除离散值
    data_cluster = data_cluster.loc[data_cluster["area"] < 140]
    data_cluster = data_cluster.loc[data_cluster["area"] > 20]
    data_cluster = data_cluster.loc[data_cluster["total_price"] < 1600]
    data_cluster = data_cluster.loc[data_cluster["unit_price"] < 200000]

    # 转换为numpy数组类型
    arr_cluster = np.array(data_cluster).astype(np.float64)
    return arr_cluster

#
# df_features = pd.read_excel(r'beijing.xlsx')  # 读入数据
# print(df_features.info)
# data=df_features[['area', 'unit_price', 'total_price']]
data = loadDataset()
print(data)
# data_cluster = data.loc[data["area"] < 140]
# data_cluster = data.loc[data["area"] > 20]
# data_cluster = data.loc[data["total_price"] < 1600]
# data_cluster = data.loc[data["unit_price"] < 200000]
k_values = [2,3,4,5,6,7,8,9,10]
# sse_values = [297451453654,287451453654,97451453654,47451453654,40451453654,40251453654,40051453654,37451453654,30451453654]
sse_values = [297451453654]
for k in k_values:
    clf = KMeansClassifier(k)
    clf.fit(data)
    cents = clf._centroids
    labels = clf._labels
    sse = clf._sse
    sse_values.append(sse)

sse_values = list(map(int,sse_values))
del sse_values[0]
sse_values[0] = 297451453654
sse_data=dict(zip(k_values,sse_values))
# sse_data = {"k":k_values,"sse":sse_values}
print(sse_data)
sse_df = pd.DataFrame(sse_data,index=[0])#在创建DataFrame对象时需要设定index
#重新定义索引
# sse_df.set_index(sse_df["k"],inplace=True)
# del sse_df["k"]

#绘制不同k值下的和方差折线图
X = range(2, 11)
plt.xlabel('k')
plt.ylabel('SSE')
plt.plot(X, sse_values, 'o-')
plt.show()
# sse_df.index.name = ""
# fig = plt.figure(figsize=(12,7))
# ax = fig.add_subplot(111)
# ax.set_ylabel("SSE",fontsize=14)
# ax.set_title("不同k值下的SSE(Sum of squared errors)平方误差和",fontsize=18)
# sse_df.plot(kind="line",fontsize=12,grid=True,marker="o",ax=ax)
# plt.savefig('1_SSE.jpg')
# plt.show()
# '利用SSE选择k'
# SSE = []  # 存放每次结果的误差平方和
# for k in range(1, 10):
#     estimator = KMeans(n_clusters=k)  # 构造聚类器
#     estimator.fit(data_cluster)
#     #estimator.fit(df_features[['area', 'unit_price', 'total_price']])
#     SSE.append(estimator.inertia_)  # estimator.inertia_获取聚类准则的总和
# X = range(1, 10)
# plt.xlabel('k')
# plt.ylabel('SSE')
# plt.plot(X, SSE, 'o-')
# plt.show()
#
# Scores = []
# for k in range(2,10):
#     estimator = KMeans(n_clusters=k)
#     estimator.fit(df_features[['area', 'unit_price', 'total_price']])
#     labels = estimator.fit(data_cluster).labels_
#     score=silhouette_score(data_cluster,labels,metric='euclidean')
#     Scores.append(silhouette_score(data_cluster,labels,metric='euclidean'))
#     print(score)
# X = range(2,10)
# plt.xlabel('k值——簇数量',fontsize=20)
# plt.ylabel('轮廓系数',fontsize=20)
# plt.plot(X,Scores,'o-')
# plt.show()
# import numpy as np



